package com.jida.tijian.service;

import com.jida.tijian.domain.Users;
import com.jida.tijian.dto.ResponseObject;
import com.jida.tijian.exception.TijianAPPException;
import com.jida.tijian.exception.TijianAPPExceptionEnum;
import com.jida.tijian.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*
 * @用户模块业务层代码
 */
@Service
public class UsersServicelmpl implements UsersService {

    @Autowired
    UsersMapper usersMapper;

    /*@Override
    public ResponseObject<Users> login(Users users){
        ResponseObject<Users> responseObject = null;
        //1.先查
        Users result = usersMapper.selectByPrimaryKey(users.getUserId());

        //2.看有没有
        if (result  ==  null){
            //告诉前端
            responseObject = new ResponseObject<>(201,"手机号尚未注册");
            return responseObject;

        }
        //3.比对密码
        if(!result.getPassword().equals(users.getPassword())){
            responseObject = new ResponseObject<>(202,"密码错误");
            return responseObject;

        }
        responseObject = new ResponseObject<>(ResponseObject.SUCCESS_STATUS,result,ResponseObject.SUCCESS_DESC);


        return responseObject;
    }
}

     */
    @Override
    public ResponseObject<Users> login(Users users){
        ResponseObject<Users> responseObject = null;
        //1.先查
        Users result = usersMapper.selectByPrimaryKey(users.getUserId());

        //2.看有没有
        if (result  ==  null){
            //告诉前端
            throw new TijianAPPException(TijianAPPExceptionEnum.MOBILE_NOT_REGISTER.getCode(), TijianAPPExceptionEnum.MOBILE_NOT_REGISTER.getMessage());

        }
        //3.比对密码
        if(!result.getPassword().equals(users.getPassword())){
            throw new TijianAPPException(TijianAPPExceptionEnum.USERID_OR_PASSWORD_IS_WRONG.getCode(), TijianAPPExceptionEnum.USERID_OR_PASSWORD_IS_WRONG.getMessage());


        }
        result.setPassword("");
        responseObject = new ResponseObject<>(ResponseObject.SUCCESS_STATUS,result,ResponseObject.SUCCESS_DESC);


        return responseObject;
    }
}

